import { createRouter, createWebHashHistory } from "vue-router"
import Login from "../pages/login/login.vue"
import Main from "../pages/main.vue"
import Reg from '../pages/reg/reg.vue';
import Shop from "../pages/shop.vue"
import Mes from "../pages/mes.vue"
import E404 from "../pages/error.vue"
import userMes from "../pages/userMes.vue"
const router = createRouter({
    history: createWebHashHistory(),
    linkActive: "router-active",
    routes: [
        {
            path: "/",
            redirect: "/login"
        }, {
            path: "/login",
            component: Login
        }, {
            path: "/reg",
            component: Reg
        },
        {
            path: "/main",
            component: Main,
            redirect: "/main/mes",
            children: [{
                path: "shop",
                component: Shop
            }, {
                path: "mes",
                component: Mes
            }, {
                path: "error",
                component: E404
            }, {
                path: "mes/:id",
                component: userMes,
                props: true
            }]
        },


    ]
})
// 控制访问权限
router.beforeEach((to, from, next) => {
    // 如果访问的登录页面直接放行
    if (to.path === "/login") return next()
    // if (to.path !== "/reg") return next("/reg")
    // 获取token
    const token = localStorage.getItem("token");
    if (!token) {
        // token不存在，强制跳转到登录页
        return next("/login")
    }
    next();
})
export default router;